package com.assist.beans;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.assist.model.Activity;
import com.assist.model.Category;
import com.assist.model.User;
import com.assist.service.IActivityService;
import com.assist.service.IUserService;
import com.assist.service.impl.ActivityServiceImpl;
import com.assist.service.impl.UserServiceImpl;

@ManagedBean(name = "createActivityBean")
@SessionScoped
public class CreateActivityBean implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String   name;
	private String   description;
	private Category selectedCategory;
	private Date     startDate;
	private Date     endDate;
	
	private IActivityService activityService;
	
	public CreateActivityBean() {
		// TODO Auto-generated constructor stub
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Category getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedCategory(Category selectedCategory) {
		this.selectedCategory = selectedCategory;
	}

	
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	
	public void saveActivity(ActionEvent e) {
		System.out.println("Save Activity clicked!");
		activityService = new ActivityServiceImpl();
		
		FacesContext context = FacesContext.getCurrentInstance(); 
		HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();  
		HttpSession httpSession = request.getSession(false);  

		// To get the attribute
		String loginUserName= (String) httpSession.getAttribute("loginUserName");
				
		Activity activity = new Activity(new Date(), new Date(), this.name, loginUserName , this.description, 
			this.startDate,this.endDate, this.selectedCategory.getName(), "New"	);
		
	    boolean result=activityService.saveOrUpdateActivity(activity);
	    if (result) {        
			System.out.println("Activity created!");
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	    } else {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "System Error!", "Activity not saved, check log file!");  	              
            RequestContext.getCurrentInstance().showMessageInDialog(message); 
	    }
				
	}
	
}
